//
//  APLogManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/15.
//

#import "APLogManager.h"
#import "APUUIDManager.h"
#import "APLoginManager.h"
#import "NSString+Category.h"
@interface APLogManager ()
@property (nonatomic, copy) NSString *launcherId;
@property (nonatomic, assign) NSInteger serialId;

@end

@implementation APLogManager

static const NSString *apData = @"data";
static const NSString *apLogType =@"log_type";
static const NSString *apSubtype =@"subtype";
static const NSString *apBehavior =@"behavior";
static const NSString *apDeviceId =@"device-id";
static const NSString *apAndroidId =@"android_id";
static const NSString *apUserId =@"user_id";
static const NSString *apPkg =@"pkg";
static const NSString *apChnId =@"chn_id";
static const NSString *apVer =@"ver";
static const NSString *apPlatform =@"platform";
static const NSString *apModel =@"model";
static const NSString *apPVer =@"p_ver";
static const NSString *apLanId =@"lan_id";
static const NSString *apSecId =@"sec_id";
static const NSString *apUtmSource =@"utm_source";
static const NSString *apAfAdgroupId =@"af_adgroup_id";
static const NSString *apAfAdset =@"af_adset";
static const NSString *apAfAdsetId =@"af_adset_id";
static const NSString *apAfStatus =@"af_status";
static const NSString *apAfAgency =@"af_agency";
static const NSString *apAfChannel =@"af_channel";
static const NSString *apAfCampaign =@"campaign";
static const NSString *apAfCampaignId =@"campaign_id";
static const NSString *apCountry =@"country";
static const NSString *apsysLan =@"sys_lan";

static const NSString *_event = @"event";
static const NSString *_tm = @"tm";
static const NSString *_page = @"page";
static const NSString *_subPage = @"subPage";
static const NSString *_terPage = @"terPage";
static const NSString *_broadcasterId = @"broadcasterId";
static const NSString *_duration = @"duration";

+ (instancetype)shareInstance{
    static APLogManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
        shareId.launcherId = [NSString stringWithFormat:@"%@%@",[NSDate date].timestamp,[APUUIDManager dm_getUUID]];
        shareId.serialId = 0;
    });
    return shareId;
}


+ (void)statisticWithPage:(NSString *)pageName{
    [APLogManager statisticWithPage:pageName subPage:nil terPage:nil broadcasterId:nil];
}


+ (void)statisticWithPage:(NSString *)pageName subPage:(NSString *)subPage terPage:(NSString *)terPage broadcasterId:(NSString *)broadcasterId{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setNullObject:pageName forKey:_page];
    [parameters setNullObject:subPage forKey:_subPage];
    [parameters setNullObject:terPage forKey:_terPage];
    [parameters setNullObject:broadcasterId forKey:_broadcasterId];
    [parameters setNullObject:@"pv" forKey:_event];
    [parameters setNullObject:@([NSDate date].timeIntervalSince1970)forKey:_tm];
    [APLogManager uploadUseLog:parameters];
}



+ (void)uploadUseLog:(NSDictionary *)event {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setNullObject:@[event] forKey:apData];
    [parameters setNullObject:@"event" forKey:apLogType];
    [parameters setNullObject:@"global_behavior" forKey:apSubtype];
    [parameters setNullObject:@"event" forKey:apBehavior];
    [parameters setNullObject:[APUUIDManager dm_getUUID] forKey:apDeviceId];
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    if (userModel) {
        [parameters setNullObject:userModel.userId forKey:apUserId];
        [parameters setNullObject:userModel.country forKey:apCountry];
    }
    [parameters setNullObject:[NSBundle mainBundle].bundleIdentifier forKey:apPkg];
    [parameters setNullObject:@"" forKey:apChnId];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    [parameters setNullObject:app_Version forKey:apVer];
    [parameters setNullObject:@"iOS" forKey:apPlatform];
    UIDevice *device = [UIDevice currentDevice];
    [parameters setNullObject:device.model forKey:apModel];
    NSString *buildNumber = infoDictionary[@"CFBundleVersion"];
    [parameters setNullObject:buildNumber forKey:apPVer];
    [parameters setNullObject:[APLogManager shareInstance].launcherId forKey:apLanId];
    [parameters setNullObject:@([APLogManager shareInstance].serialId++) forKey:apSecId];
    NSString *lan = [NSLocale currentLocale].languageCode;
    [parameters setNullObject:lan forKey:apsysLan];
    [parameters setNullObject:@([NSDate date].timeIntervalSince1970) forKey:@"tm"];
    NSString *urlString = [NSString stringWithFormat:@"%@%@",LOG_URL,logLiveChat];
    [APDataManager postJsonWithUrlString:urlString timeoutInterval:0 parameters:@[parameters] result:nil failBlock:nil];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    AFJSONResponseSerializer *response =  [AFJSONResponseSerializer serializer];
    response.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
//    response.removesKeysWithNullValues = YES;
    manager.responseSerializer = response;
    manager.requestSerializer.timeoutInterval = 10;
    [manager POST:urlString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {}];
}

@end
